/**
 * @Description: api管理
 * @Author BPF
 * @create 2021/9/1 11:20
 */
import { Vite, Type } from '@bianpengfei/utils'
import { requestParams } from './helper'

const { requireAllModule } = Vite
const { isObject } = Type

const apisModules = requireAllModule({
  requireContext: import.meta.globEager('./modules/*.js'),
  exclude: ['./modules/index.js']
})
  .reduce((pre, cur) => {
    if (cur?.default) pre.push(cur.default)
    return pre
  }, [])
  .flat(Infinity)
  .reduce((pre, cur) => {
    if (isObject(cur)) {
      pre = { ...pre, ...cur }
    }
    return pre
  }, {})

export default {
  ...apisModules,
  install: app => {
    app.config.globalProperties.$api = apisModules
    app.prototype.$requestParams = requestParams
  }
}
